/* Exercise 7.4 Lock timer */ /* Created David Miles April 2006 */ /* Updated Ben Rowland Febuary 2014 */ #include // CONFIG1 #pragma config FOSC = HS // Oscillator Selection (HS Oscillator, High-speed crystal/resonator connected between OSC1 and OSC2 pins) #pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled) #pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled) #pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR) #pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled) #pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled) #pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled) #pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin) #pragma config IESO = OFF // Internal/External Switchover (Internal/External Switchover mode is disabled) #pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled) // CONFIG2 #pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off) #pragma config VCAPEN = OFF // Voltage Regulator Capacitor Enable (All VCAP pin functionality is disabled) #pragma config PLLEN = OFF // PLL Enable (4x PLL disabled) #pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset) #pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.) #pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming) #define _XTAL_FREQ 19660800 // Defines the hardware crystal frequency allowing the delay function to work correctly #define DISPLAY_SIZE 4 //Number of LEDs in our display #define DPOINT 0x7f #define NO_DPOINT 0xff #define NO_DIGIT 255 #define DIGITS 4 #define EEPROMLOCATION 0x01 //A value for each bit in PORTA. We can feed in the number of the LED and it will give us the value to put into A const unsigned char enable [4] = { 1, 2, 4, 8 } ; //These are the patterns for the LEDs which were worked out from the datasheet. //Note that to light a LED the bit on PORTB must be low I can use this array to convert from a digit to the 7 segments needed const unsigned char patterns [10] = { 0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x83, 0xf8, 0x80, 0x98 } ; // 0 1 2 3 4 5 6 7 8 9 unsigned char segments [DISPLAY_SIZE] ; //Segment patterns for our LEDs unsigned char led_counter = 0 ; //Counter used by our refresh unsigned char counter = 0; //Timer interrupt counter unsigned char newportb; //new version of PORTB unsigned char oldportb; //old version of PORTB unsigned char portbinputs; //local copy of portb int door_timer = 0 ; //counts down to time the door lock. If the number is bigger than 0 we fire the lock output and count down unsigned char door_bit ; //used to set the bit to be ORed with PORTA to give the door status void setup_hardware (void) { ANSELA = 0x00; //Set PORTA to Digital Mode - Defaults to analogue mode ANSELB = 0x00; //Set PORTB to Digital Mode - Defaults to analogue mode OPTION_REG = 0xC0; TRISA = 0xE0; //Set bits 0-3 PORTA to be configured as an output TRISB = 0x00; //Set all bits of PORTB to be configured as an output INTCON = 0b10100000; //Configure INTCON register to enable timer interrupts OPTION_REG = 0b11000100; //Configure the timer to use the prescaler 1:32 - 19660800 / 4 / 32 / 256 = 600Hz } //Each time refresh is called it sets the display for a led and moves on to the next void refresh ( void ) { if ( door_timer > 0 ) //need to hold the lock open? { door_bit = 0x10 ; //set the bit to turn on the lock door_timer = door_timer - 1 ; //drop the timer counter } else { door_bit = 0 ; //if the timer is 0 turn the door off } LATA = door_bit ; //turn off all the LEDS but leave on the door bit LATB = 0; //Now read PORTB TRISB = 0xff; newportb = PORTB; TRISB = 0x00; LATB = segments[led_counter]; //Set segments for the led LATA = enable[led_counter] | door_bit; //Turn the led on and include the lock bit led_counter = led_counter + 1 ; //Move on to the next led if ( led_counter == DISPLAY_SIZE ) //see if we fell off the end { led_counter = 0 ; } if ( newportb == oldportb ) //Do the debounce { portbinputs = newportb; } oldportb = newportb; //Store the old value for next time } //Display just loads the pattern into the segment. refresh will read it later void display ( unsigned char digit, unsigned char pos, unsigned char mask ) { segments[pos] = patterns[digit] & mask; } //TMR0 Overflow handler void tmrHandler( void ) { refresh(); } //Allows a numeric variable to be printed to the 4 x 7-seg LEDs void display_value ( int value ) { display ( value % 10, 3, NO_DPOINT ) ; //display the units value = value / 10 ; display ( value % 10, 2, NO_DPOINT ) ; //display the tens value = value / 10 ; display ( value % 10, 1, NO_DPOINT ) ; //display the hundreds value = value / 10 ; display ( value % 10, 0, NO_DPOINT ) ; //display the thousands } //This is the interrupt handler which is called when the PIC detects an interrupt //It checks the status bits to find out who caused the interrupt and then calls that handler void interrupt isr( void ) { if(INTCON & 0b00000100) //if the timer has overflowed bit 2 of INTCON is set high { INTCON = INTCON & 0b11111011; //Clear bit 2 to turn off this interrupt counter = counter + 1; //Increment interrupt counter if ( counter > 2 ) //If counter is greater than 2 { counter = 0; //Reset counter to 0 tmrHandler(); //Call the interrupt handler function } } } void write_eeprom ( unsigned char address, unsigned char data) { while (EECON1bits.WR); //Wait for any outstanding writes to complete EEADR = address; //Set the address and data EEDATA = data; EECON1bits.EEPGD = 0; //Make sure that we're talking the the EEPROM EECON1bits.WREN = 1; //Enable writes INTCONbits.GIE = 0; //Disable interrupts EECON2 = 0x55; //Send the magic sequence to enable writes EECON2 = 0xAA; EECON1bits.WR = 1; //Set the write going INTCONbits.GIE = 1; //Renable interrupts EECON1bits.WREN = 0; //Disable writes } unsigned char read_eeprom (unsigned char address) { unsigned char data = 0; EEADR = address; //Tell the chip where to read from EECON1bits.EEPGD = 0; //We want to read from EEPROM, not the program EECON1bits.RD = 1; //Send the read command data = EEDATA; //Copy across the data return data ; //Return the result } //Get_digit reads a digit from the keypad. If no digit is pressed when it is called it returns immediately with the //value NO_DIGIT. If a digit is pressed it returns with the digit value we use the "ready debounced" value in portb unsigned char get_digit ( void ) { unsigned char i, mask ; if ( portbinputs == 0 ) { return NO_DIGIT ; //if no key is down - return immediately } mask = 1 ; //find out which bit of portb is set for ( i=0 ; i<8 ; i=i+1 ) { if ( portbinputs & mask ) { return i ; //i holds the number of the key to send back } mask = mask << 1 ; //shift on to the next bit } return NO_DIGIT ; //if we get here portb must have cleared while we were looking for the bits - in which case we return an empty value } //get_value returns an integer value from the keypad. It reads up to 4 digits. int get_value ( void ) { int total = 0 ; unsigned char i, digit ; for ( i=0 ; i < DIGITS ; i = i + 1 ) { while (1) //wait for a digit { digit = get_digit () ; if ( digit != NO_DIGIT ) { break ; //get out if we have a digit to look at } } total = total * 10 ; //update the new total total = total + digit ; display_value ( total ); //display it while ( get_digit () != NO_DIGIT ) ; //wait for the key to go up } return total ; } int main (void) { setup_hardware(); while (1) //Loop forever { while (portbinputs == 0) //wait for an input on portb { display_value (door_timer); } //set a value on door_timer to cause the door to open door_timer = 400 ; //because the counter updates at 200 Hz this should hold the door for two seconds } return 0; }